(function($, window, PhotoSwipe) {
  function adjustCaption(e) {
    var toolbar = this.toolbar;
    var $captionContentEl = $(toolbar.captionContentEl);
    $captionContentEl.html(decodeURIComponent(toolbar.currentCaption));
    window.setTimeout(function() { 
      $captionContentEl.trigger('create');
      jfPD.controller.enhancePage($captionContentEl);
      toolbar.resetPosition();
      makeCaptionScrollable($(toolbar.captionEl));
      $.mobile.activePage.trigger('jfPDExtImageGallery:captionUpdated', [$captionContentEl]);
    }, 150);
  }

  function makeCaptionScrollable($caption) {
    $caption.attr('data-scroll', 'y');
		$caption.scrollview({ direction: 'y' });
  }
  
  function onPause() {
    var $page = jfPD.controller.getCurrentContainer();
    if ($page) {
      var photoSwipeInstance = PhotoSwipe.getInstance($page.attr('id'));
      if (photoSwipeInstance) {
        try {
          photoSwipeInstance.hide()
        } catch (e) { /* already hidden */ }
      }
    }
  }
  
  $("div:jqmData(role='page')").live('pageshow', function() {
    var $currentPage = $(this);
    var $thumbs = $currentPage.find('.jfPDExtImageGallery a');
    if ($thumbs.length) {
    
      var defaultOptions = {
        jQueryMobile: true,
        backButtonHideEnabled: false, //if enabled back button wont work in phonegap apps
        captionAndToolbarFlipPosition: true,
        captionAndToolbarAutoHideDelay: false
      }
      var options = window.jfPDExtPhotoSwipeOptions || {};
      options = $.extend(true, defaultOptions, options);

    
      var photoSwipeInstance = $thumbs.photoSwipe(options, $currentPage.attr('id'));

      if (window.plugins && window.plugins.nativeControls) {
        photoSwipeInstance.addEventHandler(PhotoSwipe.EventTypes.onShow, function() { window.plugins.nativeControls.hideTabBar(); });
        photoSwipeInstance.addEventHandler(PhotoSwipe.EventTypes.onHide, function() { window.plugins.nativeControls.showTabBar(); });
      }

      photoSwipeInstance.addEventHandler(PhotoSwipe.EventTypes.onCaptionAndToolbarShow, adjustCaption);
      photoSwipeInstance.addEventHandler(PhotoSwipe.EventTypes.onDisplayImage, adjustCaption);
      
      $currentPage.find("div:jqmData(role='content')").css('overflow-y', 'hidden');
    }

  })
  .live('pagehide', function(e){
      
    var 
      $currentPage = $(this),
      photoSwipeInstance = PhotoSwipe.getInstance($currentPage.attr('id'));

    if (typeof photoSwipeInstance != "undefined" && photoSwipeInstance != null) {
      try {
        photoSwipeInstance.hide()
      } catch (e) { /* already hidden */ }
      PhotoSwipe.detatch(photoSwipeInstance);
    }
  });
  
  $(document).delegate('div.ps-caption-content a', 'vclick', function() { 
    PhotoSwipe.getInstance($.mobile.activePage.attr('id')).hide(); 
  });
  
  document.addEventListener("pause", onPause, false);

})(jQuery, window, window.Code.PhotoSwipe);
